package com.yixiang.np.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

/**
 * 获取某一时间段特定星期几的日期
 */
public class WeekDayUtil {
	public static void main(String[] args) {
        getDates("2017-01-01", "2018-01-01","星期一",4);
    }

	/**
	 * 获取某一时间段特定星期几的日期
	 * @param dateFrom 开始时间
	 * @param dateEnd 结束时间
	 * @param weekDays 星期
	 * @return 返回时间数组
	 */
    public static String[] getDates(String dateFrom, String dateEnd, String weekDays, int nextdays) {
        long time = 1l;
        long perDayMilSec = 24 * 60 * 60 * 1000;
        List<String> dateList = new ArrayList<String>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        //需要查询的星期系数
        String strWeekNumber = weekForNum(weekDays);
        try {
			dateFrom = sdf.format(sdf.parse(dateFrom).getTime() - perDayMilSec);
			while (true) {
					time = sdf.parse(dateFrom).getTime();
					time = time + perDayMilSec;
					Date date = new Date(time);
					dateFrom = sdf.format(date);
					Date weekdate = new Date(time);
				    Calendar calendar = new GregorianCalendar(); 
				    calendar.setTime(weekdate); 
				    calendar.add(calendar.DATE,nextdays);//把日期往后增加一天.整数往后推,负数往前移动 
				    weekdate=calendar.getTime();   //这个时间就是日期往后推一天的结果
				    String weekEnd = sdf.format(weekdate);
					if (dateFrom.compareTo(dateEnd) <= 0 && weekEnd.compareTo(dateEnd)<=0) {
						//查询的某一时间的星期系数
						Integer weekDay = dayForWeek(date);                    
						//判断当期日期的星期系数是否是需要查询的
						if (strWeekNumber.indexOf(weekDay.toString())!=-1) {
							System.out.println(dateFrom + "#" + weekEnd);
							dateList.add(dateFrom + "#" + weekEnd);
						}
					} else {
						break;
					}
			}
		} catch (ParseException e1) {
			e1.printStackTrace();
		}
        String[] dateArray = new String[dateList.size()];
        dateList.toArray(dateArray);
        return dateArray;
    }
    
    public static String[] getStartDates(String dateFrom, String dateEnd, String weekDays) {
        long time = 1l;
        long perDayMilSec = 24 * 60 * 60 * 1000;
        List<String> dateList = new ArrayList<String>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        //需要查询的星期系数
        String strWeekNumber = weekForNum(weekDays);
        try {
			dateFrom = sdf.format(sdf.parse(dateFrom).getTime() - perDayMilSec);
			while (true) {
					time = sdf.parse(dateFrom).getTime();
					time = time + perDayMilSec;
					Date date = new Date(time);
					dateFrom = sdf.format(date);
					Date weekdate = new Date(time);
					if (dateFrom.compareTo(dateEnd) <= 0) {
						//查询的某一时间的星期系数
						Integer weekDay = dayForWeek(date);                    
						//判断当期日期的星期系数是否是需要查询的
						if (strWeekNumber.indexOf(weekDay.toString())!=-1) {
							System.out.println(dateFrom);
							dateList.add(dateFrom);
						}
					} else {
						break;
					}
			}
		} catch (ParseException e1) {
			e1.printStackTrace();
		}
        String[] dateArray = new String[dateList.size()];
        dateList.toArray(dateArray);
        return dateArray;
    }
    //等到当期时间的周系数。星期日：1，星期一：2，星期二：3，星期三：4，星期四：5，星期五：6，星期六：7
    public static Integer dayForWeek(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.DAY_OF_WEEK);
    }
    
    /**
     * 得到对应星期的系数  星期日：1，星期一：2，星期二：3，星期三：4，星期四：5，星期五：6，星期六：7
     * @param weekDays 星期格式  星期一|星期二
     */
    public static String weekForNum(String weekDays){
    	//返回结果为组合的星期系数
    	String weekNumber = "";
    	//解析传入的星期
    	if(weekDays.indexOf("|")!=-1){//多个星期数
    		String []strWeeks = weekDays.split("\\|");
    		for(int i=0;i<strWeeks.length;i++){
    			weekNumber = weekNumber + "" + getWeekNum(strWeeks[i]).toString();
    		}
    	}else{//一个星期数
    		weekNumber = getWeekNum(weekDays).toString();
    	}
    	
    	return weekNumber;
    	
    }
    
    //将星期转换为对应的系数  星期日：1，星期一：2，星期二：3，星期三：4，星期四：5，星期五：6，星期六：7
    public static Integer getWeekNum(String strWeek){
    	Integer number = 1;//默认为星期日
    	if("星期日".equals(strWeek)){
    		number = 1;
    	}else if("星期一".equals(strWeek)){
    		number = 2;
    	}else if("星期二".equals(strWeek)){
    		number = 3;
    	}else if("星期三".equals(strWeek)){
    		number = 4;
    	}else if("星期四".equals(strWeek)){
    		number = 5;
    	}else if("星期五".equals(strWeek)){
    		number = 6;
    	}else if("星期六".equals(strWeek)){
    		number = 7;
    	}else{
    		number = 1;
    	}
    	return number;
    }

    //将星期转换为对应的系数  星期日：1，星期一：2，星期二：3，星期三：4，星期四：5，星期五：6，星期六：7
    public static String getWeekName(String week){
    	String number = "星期一";//默认为星期日
    	if("1".equals(week)){
    		number = "星期一";
    	}else if("2".equals(week)){
    		number = "星期二";
    	}else if("3".equals(week)){
    		number = "星期三";
    	}else if("4".equals(week)){
    		number = "星期四";
    	}else if("5".equals(week)){
    		number = "星期五";
    	}else if("6".equals(week)){
    		number = "星期六";
    	}else if("7".equals(week)){
    		number = "星期七";
    	}else{
    		number = "星期一";
    	}
    	return number;
    }
}